<?php

namespace apiend\modules\user\controllers;

use Yii;
use apiend\components\Controller;
use apiend\modules\user\models\FollowForm;
use apiend\modules\user\models\FollowTeacherSearch;
use apiend\modules\user\models\FollowSchoolSearch;

/**
 * 用户关注相关接口
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class FollowController extends Controller {

    public $modelClass = 'apiend\modules\user\models\FollowSearch';

    /**
     * {@inheritdoc}
     */
    public function actions() {
        return [
            'view' => [
                'class' => 'yii\rest\ViewAction',
                'modelClass' => $this->modelClass,
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            'attention' => ['POST'],
            'remark' => ['POST'],
            'ids' => ['GET'],
        ];
    }

    /**
     * 关注的教师列表
     * @inheritdoc
     */
    public function actionTeacher() {
        $model = new FollowTeacherSearch();
        return $model->search($this->token->user_id);
    }

    /**
     * 关注的教师列表
     * @inheritdoc
     */
    public function actionSchool() {
        $model = new FollowSchoolSearch();
        return $model->search($this->token->user_id);
    }

    /**
     * 关注/取消关注
     */
    public function actionAttention() {
        $model = new FollowForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            return $model->dumpError('关注请求失败，请检查参数的有效性');
        }
        $model->user_id = $this->token->user_id;
        return $model->save();
    }

    /**
     * 关注备注
     */
    public function actionRemark() {
        $model = new FollowForm();
        $model->setToken($this->token);

        return $model->remark(Yii::$app->request->post());
    }

}
